﻿#include "pch.h"

namespace fs = std::filesystem;

// 创建测试环境
class FileListTest : public ::testing::Test
{
protected:
    void SetUp() override
    {
        // 创建测试目录结构
        fs::create_directories("D:\\test_temp\\subdir1");
        fs::create_directories("D:\\test_temp\\subdir2\\empty_dir");

        // 创建测试文件
        std::ofstream("D:\\test_temp\\test1.txt") << "test";
        std::ofstream("D:\\test_temp\\test2.log") << "test";
        std::ofstream("D:\\test_temp\\subdir1\\file1.dat") << "test";

        // ============== 新增：为deleteFile测试创建专门的文件 ==============
        std::ofstream("D:\\test_temp\\to_delete.json") << "content";
        std::ofstream("D:\\test_temp\\read_only.json") << "read only";
        // 设置只读属性
        fs::permissions("D:\\test_temp\\read_only.json",
                        fs::perms::owner_read,
                        fs::perm_options::replace);
    }

    void TearDown() override
    {
        // 清理测试目录
        fs::remove_all("D:\\test_temp");
    }
};
// 测试用例1：基本功能测试
TEST_F(FileListTest, MemoryLeak)
{
    char* p = new char[1234];
}
// 测试用例1：基本功能测试
TEST_F(FileListTest, BasicFunctionality)
{
    TFileItem items[10];
    int32_t itemCount = 0;

    // 获取所有文件和目录
    bool result = getFileList("D:\\test_temp", false, items, 10, &itemCount, "*");

    ASSERT_TRUE(result);
    EXPECT_GT(itemCount, 0);
    EXPECT_LE(itemCount, 10);
}

// 测试用例2：通配符过滤
TEST_F(FileListTest, WildcardFilter)
{
    TFileItem items[10];
    int32_t itemCount = 0;

    // 只获取.txt文件
    bool result = getFileList("D:\\test_temp", false, items, 10, &itemCount, "*.txt");

    ASSERT_TRUE(result);
    EXPECT_EQ(itemCount, 1);
    if (itemCount > 0)
    {
        EXPECT_STREQ(items[0].fileName, "test1");
        EXPECT_STREQ(items[0].extName, ".txt");
    }
}

// 测试用例3：包含子目录
TEST_F(FileListTest, IncludeSubdirectories)
{
    TFileItem items[10];
    int32_t itemCount = 0;

    // 包含子目录
    bool result = getFileList("D:\\test_temp", true, items, 10, &itemCount, "*");

    ASSERT_TRUE(result);
    EXPECT_GE(itemCount, 4); // 至少包含4个条目
}

// 测试用例4：缓冲区不足
TEST_F(FileListTest, InsufficientBuffer)
{
    TFileItem items[2]; // 故意设置小缓冲区
    int32_t itemCount = 0;

    bool result = getFileList("D:\\test_temp", true, items, 2, &itemCount, "*");

    EXPECT_FALSE(result);
    EXPECT_GT(itemCount, 2); // 实际数量应大于缓冲区容量
}

// 测试用例5：空目录
TEST_F(FileListTest, EmptyDirectory)
{
    TFileItem items[10];
    int32_t itemCount = 0;

    // 测试空目录
    bool result = getFileList("D:\\test_temp\\subdir2\\empty_dir", false, items, 10, &itemCount, "*");

    ASSERT_TRUE(result);
    EXPECT_EQ(itemCount, 0);
}

// 测试用例6：不存在的路径
TEST_F(FileListTest, NonExistentPath)
{
    TFileItem items[10];
    int32_t itemCount = 0;

    bool result = getFileList("D:\\nonexistent", false, items, 10, &itemCount, "*");

    EXPECT_FALSE(result);
}

// 测试用例7：文件属性验证
TEST_F(FileListTest, FileAttributes)
{
    TFileItem items[10];
    int32_t itemCount = 0;

    bool result = getFileList("D:\\test_temp", false, items, 10, &itemCount, "test1.txt");

    ASSERT_TRUE(result);
    ASSERT_EQ(itemCount, 1);
    EXPECT_FALSE(items[0].isFolder);
    EXPECT_GT(items[0].fileSize, 0);
    EXPECT_GT(items[0].createTime, 0);
    EXPECT_GT(items[0].modifyTime, 0);
}

// 测试用例8：目录属性验证
TEST_F(FileListTest, DirectoryAttributes)
{
    TFileItem items[10];
    int32_t itemCount = 0;

    bool result = getFileList("D:\\test_temp", true, items, 10, &itemCount, "subdir1");

    ASSERT_TRUE(result);
    ASSERT_EQ(itemCount, 1);
    EXPECT_TRUE(items[0].isFolder);
    EXPECT_EQ(items[0].fileSize, 0);
    EXPECT_GT(items[0].createTime, 0);
    EXPECT_GT(items[0].modifyTime, 0);
}

// 测试用例9：正常删除文件
TEST_F(FileListTest, DeleteFileSuccess)
{
    const char *fileToDelete = "D:\\test_temp\\to_delete.json";

    // 确保文件存在
    ASSERT_TRUE(fs::exists(fileToDelete));

    // 删除文件
    EXPECT_TRUE(deleteFile(fileToDelete));

    // 验证文件已被删除
    EXPECT_FALSE(fs::exists(fileToDelete));

    // 验证错误码应为0（成功）
    int32_t errorCode = getLastErrorCode();
    char *errorMsg = getLastErrorMessage();
    EXPECT_EQ(errorCode, 0);
    EXPECT_STREQ(errorMsg, "");
}

// 测试用例10：删除不存在的文件
TEST_F(FileListTest, DeleteNonExistentFile)
{
    const char *nonExistentFile = "D:\\test_temp\\not_exist.txt";

    // 确保文件不存在
    ASSERT_FALSE(fs::exists(nonExistentFile));

    // 尝试删除
    EXPECT_FALSE(deleteFile(nonExistentFile));

    // 验证错误码
    int32_t errorCode = getLastErrorCode();
    char *errorMsg = getLastErrorMessage();
    EXPECT_EQ(errorCode, ERR_CODE_FILE_NOT_EXISTS);
    EXPECT_NE(std::string(errorMsg).find("deleteFile"), std::string::npos);
}

// 测试用例11：尝试删除目录
TEST_F(FileListTest, DeleteDirectoryShouldFail)
{
    const char *dirToDelete = "D:\\test_temp\\subdir1";

    // 确保是目录
    ASSERT_TRUE(fs::is_directory(dirToDelete));

    // 尝试删除（应失败）
    EXPECT_FALSE(deleteFile(dirToDelete));

    // 验证目录仍然存在
    EXPECT_TRUE(fs::exists(dirToDelete));

    // 验证错误码
    int32_t errorCode = getLastErrorCode();
    char *errorMsg = getLastErrorMessage();
    EXPECT_EQ(errorCode, ERR_CODE_FILE_DELETE_FAILED);
}

// 测试用例12：删除只读文件（测试异常处理）
TEST_F(FileListTest, DeleteReadOnlyFile)
{
    const char *readOnlyFile = "D:\\test_temp\\read_only.json";

    // 确保文件存在且为只读
    ASSERT_TRUE(fs::exists(readOnlyFile));

    // 尝试删除（应失败）
    EXPECT_FALSE(deleteFile(readOnlyFile));

    // 验证文件仍然存在
    EXPECT_TRUE(fs::exists(readOnlyFile));

    // 验证错误码
    int32_t errorCode = getLastErrorCode();
    char *errorMsg = getLastErrorMessage();
    EXPECT_EQ(errorCode, ERR_CODE_FILE_DELETE_FAILED);
}
#ifdef TEST_FILEOP
int32_t main(int32_t argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
#endif